package com.xls.dormitoryservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.exception.BusinessException;
import com.xls.dormitoryservice.entity.Hygiene;
import com.xls.dormitoryservice.entity.Room;
import com.xls.base.model.Student;
import com.xls.dormitoryservice.mapper.HygieneMapper;
import com.xls.dormitoryservice.mapper.RoomMapper;
import com.xls.base.mapper.StudentMapper;
import com.xls.dormitoryservice.service.RoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xls.dormitoryservice.vo.RoomInfoVo;
import com.xls.dormitoryservice.vo.RoomQuery;
import com.xls.base.vo.StudentHousingVo;
import com.xls.entity.SecurityUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private HygieneMapper hygieneMapper;

    @Override
    public boolean saveRoom(Room room) {
        return this.save(room);
    }

    @Override
    public boolean updateRoom(Room room) {
        return this.updateById(room);
    }

    @Override
    public Page<Room> getRoomPage(Integer page, Integer limit, RoomQuery roomQuery) {
        QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();
        String building = roomQuery.getBuilding();
        String floor = roomQuery.getFloor();
        String rno = roomQuery.getRno();
        roomQueryWrapper
                .eq(!StringUtils.isEmpty(building), "building", building)
                .eq(!StringUtils.isEmpty(floor), "floor", floor)
                .likeRight(!StringUtils.isEmpty(rno), "rno", rno)
                .eq("is_deleted", 0);
        return roomMapper.getRoomPage(new Page<>(page, limit), roomQueryWrapper);
    }

    @Override
    public RoomInfoVo getRoomInfoVo(String roomId) {
        RoomInfoVo roomInfoVo = new RoomInfoVo();
        if ("-1".equals(roomId)) {
            SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = securityUser.getUsername();
            Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sno", username));
            if (student == null) {
                throw new BusinessException(8888, "管理员不允许操作");
            }
            if (student.getRoomId() == null) {
                throw new BusinessException(8888, "请先入住");
            }
            roomId = student.getRoomId().toString();
        }
        //根据宿舍id查询宿舍
        Room roomVo = roomMapper.getRoomVo(roomId);
        roomInfoVo.setRoom(roomVo);
        //根据宿舍id查询宿舍学生
        //List<StudentVo> studentList = studentMapper.getStudentList(roomId);
        List<StudentHousingVo> studentHousingVoList = studentMapper.getStudentHousingVoList(roomId);
        roomInfoVo.setStudentHousingVoList(studentHousingVoList);
        //根据宿舍id查询宿舍卫生
        List<Hygiene> hygieneList = hygieneMapper.getHygieneByRoomId(roomId);
        roomInfoVo.setHygieneList(hygieneList);
        return roomInfoVo;
    }

    @Override
    public void uploadRoomImage(String id, String url) {
        Room room = roomMapper.selectById(id);
        String roomUrl = room.getUrl();
        UpdateWrapper<Room> roomUpdateWrapper = new UpdateWrapper<>();
        roomUpdateWrapper.eq("id", id);
        //此时宿舍没有图片
        if (roomUrl == null || "".equals(roomUrl)) {
            roomUpdateWrapper.set("url", url);
        } else {
            roomUpdateWrapper.set("url", roomUrl + "," + url);
        }
        roomMapper.update(new Room(), roomUpdateWrapper);
    }

    @Override
    public void removeRoomImage(String id, String url) {
        Room room = roomMapper.selectById(id);
        String roomUrl = room.getUrl();
        UpdateWrapper<Room> roomUpdateWrapper = new UpdateWrapper<>();
        roomUpdateWrapper.eq("id", id);
        Set<String> urls = Arrays.stream(roomUrl.split(",")).collect(Collectors.toSet());
        urls.remove(url);
        if (urls.size() == 0) {
            roomUpdateWrapper.set("url", null);
        } else {
            roomUpdateWrapper.set("url", String.join(",",urls));
        }
        roomMapper.update(new Room(), roomUpdateWrapper);
    }

}
